<template>
  <div>
    <div>
      <ele-page
        v-loading="loading"
        :formConfig="searchConfig"
        ref="eleTable"
        :operateList="operateList"
        @handleOperateEvent="handleOperateEvent"
        :column="column"
        :getList="getList"
        uniqueId="mesLoadmaterialVerificationDetailTable"
        :otherParams="otherParams"
      >
      </ele-page>
    </div>
    <ele-form-drawer :isLoading="isLoading" v-model="formDataDialog" v-bind="formDescDialog" :request-fn="handleSubmit" :visible.sync="visible" :title="title"> </ele-form-drawer>
  </div>
</template>

<script>
import { operateList, searchConfig, formDescDialog, rowConfig } from './config'
import { getList, rowCreate, rowUpdate, rowDel, getInfo, getMasterInfo } from '@/api/loadMaterial/mesLoadMaterialVerificationDetail'

export default {
  name: 'mesLoadMaterialVerificationDetail',
  data() {
    return {
      title: null,
      visible: false,
      isLoading: false,
      loading: false,
      formDataDialog: {},
      formDescDialog, //新增修改框
      searchConfig,
      operateList,
      getList,
      otherParams: this.$route.query,
      // 列表页字段的绑定配置
      column: [
        {
          type: 'selection'
        },
        {
          type: 'index',
          label: $i18n.t('common.order'),
          fixed: 'left',
          width: 60
        },
        // {
        //   prop: "verificationId",
        //   label: "外键(mes_loadmaterial_verification)",
        // },
        // {
        //   prop: "saleCode",
        //   label: "销售订单编码",
        //   width: 150,
        // },
        {
          prop: 'planCode',
          showOverflowTooltip: true,
          label: $i18n.t('mesLoadMaterialVerificationDetail.productOrder'),
          width: 180
        },
        {
          prop: 'orderCode',
          showOverflowTooltip: true,
          label: $i18n.t('mesLoadMaterialVerificationDetail.productWorkOrder'),
          width: 200
        },
        // {
        //   prop: 'packageCode',
        //   showOverflowTooltip: true,
        //   label: $i18n.t('mesLoadMaterialVerificationDetail.packingBoxCode'),
        //   width: 200
        // },
        // {
        //   prop: 'boxCode',
        //   showOverflowTooltip: true,
        //   label: $i18n.t('mesLoadMaterialVerificationDetail.boxCode'),
        //   width: 150
        // },
        {
          prop: 'orderNo',
          showOverflowTooltip: true,
          label: $i18n.t('mesLoadMaterialVerificationDetail.orderNo'),
          width: 180
        },
        {
          prop: 'barcode',
          showOverflowTooltip: true,
          label: $i18n.t('mesLoadMaterialVerificationDetail.barcode'),
          width: 200
        },
        {
          prop: 'boxCode',
          showOverflowTooltip: true,
          label: $i18n.t('mesLoadMaterialVerificationDetail.boxCode'),
          width: 150
        },
        {
          prop: 'batchNo',
          showOverflowTooltip: true,
          label: $i18n.t('mesLoadMaterialVerificationDetail.batchNo'),
          width: 150
        },
        {
          prop: 'unit',
          label: $i18n.t('common.unit'),
          width: 150
        },
        {
          prop: 'owner',
          label: $i18n.t('mesLoadMaterialVerificationDetail.owner'),
          width: 150
        },
        {
          prop: 'version',
          showOverflowTooltip: true,
          label: $i18n.t('mesLoadMaterialVerificationDetail.version'),
          width: 150
        },
        {
          prop: 'prodCode',
          showOverflowTooltip: true,
          label: $i18n.t('mesLoadMaterialVerificationDetail.prodCode'),
          width: 150
        },
        {
          prop: 'prodName',
          showOverflowTooltip: true,
          label: $i18n.t('mesLoadMaterialVerificationDetail.prodName'),
          width: 250
        },
        //{
        //  prop: "customerCode",
        //  label: $i18n.t("mesLoadMaterialVerificationDetail.customerCode"),
        //  width: 150,
        //},
        // {
        //   prop: "remark",
        //   label: "备注",
        // },
        {
          prop: 'createByName',
          label: $i18n.t('common.createByName'),
          width: 120
        },
        {
          prop: 'createTime',
          label: $i18n.t('common.createTime'),
          width: 180
        },
        {
          prop: 'updateByName',
          label: $i18n.t('common.updateByName'),
          width: 130
        },
        {
          prop: 'updateTime',
          label: $i18n.t('common.updateTime'),
          width: 180
        },
        {
          prop: 'operate',
          label: $i18n.t('common.operation'),
          fixed: 'right',
          width: 120,
          render: (h, scope) => {
            return <ele-row-operation scope={scope} rowConfig={rowConfig} onRowOperate={this.rowOperate}></ele-row-operation>
          }
        }
      ]
    }
  },
  computed: {},
  methods: {
    //表格顶部按钮操作事件
    handleOperateEvent({ handleName, name, rows }) {
      this.title = name
      const fnc = {
        add: () => {
          getMasterInfo(this.$route.query.verificationId)
            .then((res) => {
              // this.formDataDialog = {}
              // 默认主子表的关联字段相同，若有问题，自己根据实际情况转换
              // this.formDataDialog = res.data.result
              this.changeSub(false)
              this.formDataDialog = {
                verificationId: res.data.result.id,
                verificationCode: res.data.result.verificationCode
              }
              this.visible = true
            })
            .finally(() => {
              this.isLoading = false
            })
        },
        delete: () => {
          this.$confirm('此操作将删除该上料验证明细表, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            const ids = rows.map((val) => val.id).join(',')
            this.rowDel(ids)
          })
        }
      }
      fnc[handleName] && fnc[handleName]()
    },
    //表格行操作事件
    rowOperate({ handleName, name, row }) {
      this.title = name
      const fnc = {
        show: () => {
          // 修改详情，并打开界面
          this.editInfo(row.id, true)
        },
        edit: () => {
          // 修改详情，并打开界面
          this.editInfo(row.id, false)
        }
      }
      fnc[handleName] && fnc[handleName]()
    },
    //弹框提交事件
    handleSubmit(data) {
      if (this.isLoading) return // 判断状态
      this.isLoading = true
      this.formDataDialog.id ? this.rowUpdate(data) : this.rowCreate(data)
    },
    //新增
    rowCreate(data) {
      rowCreate(data)
        .then((res) => {
          const message = res.data.message
          this.$message.success(message || $i18n.t('common.addSuccess'))
          this.visible = false
          this.refresh()
        })
        .finally(() => {
          this.isLoading = false
        })
    },
    // 修改详情
    editInfo(id, flag) {
      this.changeSub(flag)
      this.loading = true
      getInfo(id)
        .then((res) => {
          this.formDataDialog = res.data.result
          this.visible = true
        })
        .finally(() => {
          this.loading = false
        })
    },
    //修改
    rowUpdate(data) {
      rowUpdate(data)
        .then((res) => {
          const message = res.data.message
          this.$message.success(message || $i18n.t('common.editSuccess'))
          this.visible = false
          this.refresh()
        })
        .finally(() => {
          this.isLoading = false
        })
    },
    //删除
    rowDel(data) {
      this.loading = true
      rowDel(data)
        .then((res) => {
          const message = res.data.message
          this.$message.success(message || $i18n.t('common.deleteSuccess'))
          this.refresh()
        })
        .finally(() => {
          this.loading = false
        })
    },
    //刷新表格数据
    refresh(isReset = true) {
      this.$refs.eleTable.refresh(isReset)
    },
    changeSub(flag) {
      if (flag) {
        formDescDialog.disabled = true
        formDescDialog.isShowSubmitBtn = false
      } else {
        formDescDialog.disabled = false
        formDescDialog.isShowSubmitBtn = true
      }
    }
  }
}
</script>
